package com.diligent.cloud.sfc.factory.impl;

import com.canny.commons.model.Codes;
import com.canny.commons.model.ObjectResult;
import com.canny.commons.model.Result;
import com.diligent.cloud.logistics.container.model.LcmBoxFill;
import com.diligent.cloud.primary.data.feign.service.rt.RtWorkCenterFeignService;
import com.diligent.cloud.primary.data.model.rt.RtWorkCenter;
import com.diligent.cloud.sfc.factory.StationFactory;
import com.diligent.cloud.sfc.model.MatType;
import com.diligent.cloud.sfc.model.detail.OffShelvesPackagingDetailInfo;
import com.diligent.cloud.sfc.model.enums.Cwoc;
import com.diligent.cloud.sfc.model.info.OffShelvesPackagingInfo;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.sfc.service.WipService;
import com.diligent.cloud.sfc.util.ConverterUtil;
import com.diligent.cloud.wip.panel.modle.WipStorageLedger;
import com.diligent.cloud.workorder.model.WoOrder;
import com.diligent.cloud.workorder.service.WoOrderFeignService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * UnShelvePackStation class
 *
 * @author xf107278
 * @date 4/3/2020
 */
@Component
public class UnShelvePackagingStation implements StationFactory {

    private WoOrderFeignService orderFeignService;

    private ISfcService sfcService;

    private RtWorkCenterFeignService rtWorkCenterFeignService;

    private WipService wipService;

    public UnShelvePackagingStation(WoOrderFeignService orderFeignService, ISfcService sfcService,
                                    RtWorkCenterFeignService rtWorkCenterFeignService, WipService wipService) {
        this.orderFeignService = orderFeignService;
        this.sfcService = sfcService;
        this.rtWorkCenterFeignService = rtWorkCenterFeignService;
        this.wipService = wipService;
    }

    @Override
    public Result getOrderInfo(WoOrder order) {
        OffShelvesPackagingDetailInfo offShelvesPackagingDetailInfo = new OffShelvesPackagingDetailInfo();
        List<OffShelvesPackagingInfo> offShelvesInfos = new ArrayList<>();
        OffShelvesPackagingInfo offShelvesPackagingInfo = new OffShelvesPackagingInfo();
        offShelvesInfos.add(offShelvesPackagingInfo);

        List<LcmBoxFill> bz = sfcService.getOrderSameLocation(Cwoc.包装车间.getCwocCode(), order.getId(),MatType.包装物);
        Set<String> sameOrderSet = new HashSet<>();
        if (bz.size() != 0) {
            Long totalWeight = 0L;
            for (LcmBoxFill lcmBoxFill : bz) {
                totalWeight = totalWeight + lcmBoxFill.getLcmBoxFillMaster().getTotalNetWeight().longValue();

                sameOrderSet.add(lcmBoxFill.getLcmBoxFillDetail().getRefBoxCode());
            }
            offShelvesPackagingDetailInfo.setSameOrder(sameOrderSet.size());
            offShelvesPackagingDetailInfo.setSameTotalWeight(totalWeight);

            offShelvesPackagingDetailInfo.setPackagingSurplusTotalWeight
                    (getSurplusTotalWeight(order));
        }
        offShelvesPackagingDetailInfo.setOffShelvesInfos(offShelvesInfos);

        return ConverterUtil.ConverterByData(offShelvesPackagingDetailInfo, Codes.success.getCode(),
                Codes.success.getMsg());
    }


     Long getSurplusTotalWeight(WoOrder order) {
        String matCode = sfcService.getMatCodeByOrderIdAndSystemId(order.getId(),
                order.getSystemId(), MatType.包装物);
         String batchCode = sfcService.getBatchCodeByOrderId(order.getId(), matCode);

        RtWorkCenter workCenterByCwocCode = sfcService.getWorkCenterByCwocCode(order.getWorkCenterCode());

        ResponseEntity<ObjectResult> inventoryResult =
                wipService.getInventoryByMatCodeAndCwocCode(matCode,
                        workCenterByCwocCode.getCwocCode(),batchCode, order.getSystemId());

        if (inventoryResult.getBody().getData()!=null) {
            WipStorageLedger storageLedger = (WipStorageLedger) inventoryResult.getBody().getData();
            return   storageLedger.getWeight().longValue();
        } else {
            return 0L;
        }
    }
}

